layui.define('form', function(exports){ //假如该组件依赖 layui.form
    var $ = layui.$
        ,form = layui.form

        //字符常量
        ,MOD_NAME = 'switch3', ELEM = '.layui-switch3'

        //外部接口
        ,switch3 = {
            index: layui.regionSelect ? (layui.regionSelect.index + 10000) : 0

            //设置全局项
            ,set: function(options){
                var that = this;
                that.config = $.extend({}, that.config, options);
                return that;
            }

            //事件监听
            ,on: function(events, callback){
                return layui.onevent.call(this, MOD_NAME, events, callback);
            }
        }

        //操作当前实例
        ,thisIns = function(){
            var that = this
                ,options = that.config
                ,id = options.id || options.index;

            return {
                reload: function(options){
                    that.reload.call(that, options);
                }
                ,config: options
            }
        }

        //构造器
        ,Class = function(options){
            var that = this;
            that.index = ++switch3.index;
            that.config = $.extend({}, that.config, switch3.config, options);
            that.render();
        };

    //默认配置
    Class.prototype.config = {
        left_val:0,
        right_val:1,
        uncheck_val:-1,
    };

    //点击事件
    Class.prototype.action = function(){
        var that = this,options = that.config;
        var _box = that.elemTemp;
        _box.children('em').each(function(index){
            var ind = index+1
                ,othis = $(this);
            othis.on('click',function(e){
                vals = othis.parent().parent().attr('option');
                valArrs = vals.split('|');
                var checkVal = '';
                if(othis.hasClass('on')){
                    othis.removeClass('on');
                    checkVal = valArrs[0];
                }else{
                    checkVal = valArrs[ind];
                    othis.addClass('on').siblings().removeClass('on');
                }
                options.done && options.done(checkVal,othis);
            })
        });
    }

    Class.prototype.events = function(){
        var that = this,
            options = that.config;
    }

    //渲染视图
    Class.prototype.render = function(){
        var that = this
            ,options = that.config;
        $(options.elem).each(function(index,item){
            var currVal = $(item).attr('value');
            console.log('swotch-val',currVal);
            var titleArr = item.title.split('|');
            var option = $(item).attr('option');
            var optionArr = option.split('|');
            var on1 = currVal == optionArr[1] ? 'on' : '';
            var on2 = currVal == optionArr[2] ? 'on' : '';
            var temp = '<div class="layui-switch3" data-option="'+ option +'"><em class="'+ on1 +'">'+ titleArr[0] +'</em><em class="'+ on2 +'">'+ titleArr[1]+'</em></div>';
            that.elemTemp = $(temp);
            $(options.elem).eq(index).html(that.elemTemp);
            //options.elem.html(that.elemTemp);
            that.action();
        });

        /*var titleArr = $(options.elem).attr('title').split('|');
        options.elem = $(options.elem);

        //组件模板
        var temp = '<div class="layui-switch3"><em class="on">'+ titleArr[0] +'</em><em>'+ titleArr[1]+'</em></div>';
        that.elemTemp = $(temp);
        options.elem.html(that.elemTemp);
        that.action();*/
    }

    //核心入口
    switch3.render = function(options){
        var ins = new Class(options);
        return thisIns.call(ins);
    };

    //加载组件所需样式
    layui.link(layui.cache.base + 'switch3/switch3.css?v=1', function(){
        //样式加载完毕的回调

    }, 'switch3'); //此处的“regionSelect”要对应 regionSelect.css 中的样式： html #layuicss-regionSelect{}

    exports('switch3', switch3);
});